package com.chaoxin.lovehelp.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Map;
import com.chaoxin.lovehelp.common.enums.vo.activityEditVo;
import com.chaoxin.lovehelp.common.enums.vo.activityRecipientsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.chaoxin.lovehelp.service.ActivityService;
import com.chaoxin.lovehelp.common.utils.PageUtils;
import com.chaoxin.lovehelp.common.utils.R;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 *
 * @author honghu
 * @email 3027708213@qq.com
 * @date 2024-02-04 22:13:14
 */
@RestController
@CrossOrigin
@RequestMapping("lovehelp/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) throws ParseException {
        PageUtils page = activityService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 会员活动
     */
    @RequestMapping("/fellowactivitylist")
    public R fellowActivityList(HttpServletRequest httpServletRequest, @RequestParam Map<String, Object> params) throws ParseException {
        PageUtils page = activityService.getFellowActivity(httpServletRequest,params);

        return R.ok().put("page", page);
    }


    /**
     * 单个活动信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        activityEditVo vo = activityService.getEditVo(id);
        return R.ok().put("activity", vo);
    }

    //单个活动的受助者信息
    @RequestMapping("/recipientsOfActivity")
    public R recipientsOfActivity(HttpServletRequest request,@RequestParam Map<String, Object> params){
        activityRecipientsVo vo = activityService.getRecipientsOfActivity(request,params);
        return R.ok().put("recipientsOfActivity",vo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody activityEditVo vo){
        if(vo.getStartTime().after(vo.getEndTime())){
            return R.error(400,"活动开始时间不可晚于结束时间");
        }
		activityService.saveVo(vo);

        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody activityEditVo vo){
		activityService.updateVo(vo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		activityService.removeIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     *
     * 导出会员活动
     */
    @RequestMapping("/exportfellowsactivity")
    public R exportFellowsActivity(@RequestParam Map<String, Object> params) throws ParseException, UnsupportedEncodingException {
        activityService.exportFellowsActivity(params);
        return R.ok();
    }
    /**
     *
     * 导出单个活动的受助者信息
     */
    @RequestMapping("/exportrecipientsactivity/{id}")
    public R exportRecipientsActivity(@PathVariable("id") Integer id) throws IOException {
        activityService.exportRecipientsActivity(id);
        return R.ok();
    }

    //获取总志愿时长
    @RequestMapping("/allactivitytime")
    public R getAllActivityTime(@RequestParam Map<String, Object> params) throws ParseException {
        String time = activityService.getAllActivityTime(params);
        return  R.ok().put("time",time);
    }
    //获取会员活动时长
    @RequestMapping("/fellowallactivitytime")
    public R getFellowActivityTime(@RequestParam Map<String, Object> params) throws ParseException {
        String time = activityService.getFellowActivityTime(params);
        return  R.ok().put("time",time);
    }

}
